<?php

namespace App\Admin\Controllers;

use App\Models\Course;
use App\Models\Teacher;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class CourseController extends AdminController
{


    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '课程';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Course());

        $grid->column('id', 'ID');
        $grid->column('name', '课程名称');
        $grid->column('description', '课程描述');
        $grid->column('teacher.name', '老师');
        $grid->column('start_time', '开始时间');
        $grid->column('end_time', '结束时间');
        $grid->column('created_at', '创建时间');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Course::findOrFail($id));

        $show->field('id', 'ID');
        $show->field('name', '课程名称');
        $show->field('description', '课程描述');
        $show->field('teacher.name', '老师id');
        $show->field('start_time', '开始时间');
        $show->field('end_time', '结束时间');
        $show->field('created_at', '创建时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Course());
        $teachers = Teacher::pluckInfo();

        $form->text('name', '课程名称');
        $form->textarea('description', '课程描述');
//        $form->number('teacher_id', '老师id');
        $form->select('teacher_id', '老师')->options($teachers);
        $form->datetime('start_time', '开始时间')->default(date('Y-m-d H:i:s'));
        $form->datetime('end_time', '结束时间')->default(date('Y-m-d H:i:s'));

        return $form;
    }
}
